package com.nari.ncps.screen.service.impl.al;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.al.AlOpermonIuState;
import com.nari.ncps.screen.mapper.al.AlOpermonIuStateMapper;
import com.nari.ncps.screen.service.al.AlOpermonIuStateService;
import com.nari.ncps.screen.vo.al.AluminumProductionEfficiencyVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 电流电压详情表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class AlOpermonIuStateServiceImpl extends ServiceImpl<AlOpermonIuStateMapper, AlOpermonIuState> implements AlOpermonIuStateService {

    @Override
    public AluminumProductionEfficiencyVO getProductionEfficiency(String orgCode) {
        AluminumProductionEfficiencyVO vo = new AluminumProductionEfficiencyVO();
        
        // 查询最新的电流电压数据（最近20条，每20分钟一条）
        QueryWrapper<AlOpermonIuState> queryWrapper = new QueryWrapper<>();
        if (orgCode != null && !orgCode.isEmpty()) {
            queryWrapper.eq("org_code", orgCode);
        }
        queryWrapper.orderByDesc("year", "month", "day", "statistic_time")
                .last("LIMIT 20");
        
        List<AlOpermonIuState> dataList = this.list(queryWrapper);
        
        if (dataList != null && !dataList.isEmpty()) {
            // 查询300KA系列电流
            BigDecimal current300KA = getLatestValueByLevel(dataList, "300ka", (byte) 0);
            vo.setCurrent300KA(current300KA);
            
            // 查询350KA系列电流
            BigDecimal current350KA = getLatestValueByLevel(dataList, "350ka", (byte) 0);
            vo.setCurrent350KA(current350KA);
            
            // 查询300KA系列电压
            BigDecimal voltage300KA = getLatestValueByLevel(dataList, "300ka", (byte) 1);
            vo.setVoltage300KA(voltage300KA);
            
            // 查询400KA系列电压
            BigDecimal voltage400KA = getLatestValueByLevel(dataList, "400ka", (byte) 1);
            vo.setVoltage400KA(voltage400KA);
            
            // 生成实时用电负荷趋势数据（最近20条数据，每20分钟一条）
            List<AluminumProductionEfficiencyVO.PowerLoadItem> trendData = new ArrayList<>();
            
            for (AlOpermonIuState data : dataList) {
                AluminumProductionEfficiencyVO.PowerLoadItem item = new AluminumProductionEfficiencyVO.PowerLoadItem();
                item.setTime(data.getStatisticTime());
                // 用电负荷 = 电流 * 电压 / 10000 转换为万千瓦
                if (data.getValue() != null) {
                    item.setPowerLoad(data.getValue().divide(new BigDecimal("100"), 2, java.math.RoundingMode.HALF_UP));
                }
                trendData.add(item);
            }
            vo.setPowerLoadTrend(trendData);
        }
        
        return vo;
    }
    
    /**
     * 根据电流等级和监测类型获取最新值
     */
    private BigDecimal getLatestValueByLevel(List<AlOpermonIuState> dataList, String level, Byte monitorType) {
        for (AlOpermonIuState data : dataList) {
            if (level.equals(data.getCurrentLevel()) && monitorType.equals(data.getMonitorType())) {
                return data.getValue();
            }
        }
        return BigDecimal.ZERO;
    }

}
